package com.atguigu.process.controller;


import com.atguigu.common.result.Result;
import com.atguigu.model.process.ProcessType;
import com.atguigu.process.service.OaProcessTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 审批类型 前端控制器
 * </p>
 *
 * @author ws
 * @since 2024-01-16
 */
@Api(tags = "审批模板类型")
@RestController
@RequestMapping("/admin/process/processType")
public class OaProcessTypeController {

    @Resource
    private OaProcessTypeService oaProcessTypeService;

    @ApiOperation(value = "获取全部审批分类")
    @GetMapping("findAll")
    public Result findAll() {
        return Result.ok(oaProcessTypeService.list());
    }

    /**
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("{page}/{limit}")
    @ApiOperation("分页查询所有的模板")
    public Result list(@PathVariable Integer page,
                       @PathVariable Integer limit){
        Page<ProcessType> processTypePage = new Page<>(page, limit);
        IPage<ProcessType> page1 = oaProcessTypeService.selectPage(processTypePage);
        return Result.ok(page1).message("查询成功");
    }

    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        ProcessType byId = oaProcessTypeService.getById(id);
        return Result.ok(byId);
    }

    @PostMapping("save")
    @ApiOperation("添加模板")
    public Result save(@RequestBody ProcessType processType){
        boolean save = oaProcessTypeService.save(processType);
        if(!save){
            return Result.fail("模版类型添加失败");
        }
        return Result.ok();
    }

    @PostMapping("update")
    @ApiOperation("编辑模板类型")
    public Result edit(@RequestBody ProcessType processType){
        boolean result = oaProcessTypeService.updateById(processType);
        if(!result){
            return Result.fail();
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        oaProcessTypeService.removeById(id);
        return Result.ok();
    }

    @PostMapping("removeByBatch")
    @ApiOperation("编辑模板类型")
    public Result removeByBatch(@RequestParam Integer[] ids){
        boolean result = oaProcessTypeService.removeByIds(Arrays.asList(ids));
        if(!result){
            return Result.fail();
        }
        return Result.ok();
    }


}

